<template>
    <div class="main-container">
        <el-card class="box-card !border-none" shadow="never">

            <div class="flex justify-between items-center">
                <div class="detail-head !m-0">
                    <div class="left" @click="router.push('/shop/order/delivery')">
                        <span class="iconfont iconxiangzuojiantou !text-xs"></span>
                        <span class="ml-[1px]">{{ t('returnToPreviousPage') }}</span>
                    </div>
                    <span class="adorn">|</span>
                    <span class="right">{{ pageName }}</span>
                </div>
                <el-button type="primary" @click="addEvent">
                    {{ t('addTemplate') }}
                </el-button>
            </div>

            <el-card class="box-card !border-none my-[10px] table-search-wrap" shadow="never">
                <el-form :inline="true" :model="templateTable.searchParam" ref="searchFormRef">
                    <el-form-item :label="t('templateName')" prop="template_name">
                        <el-input v-model="templateTable.searchParam.template_name"
                            :placeholder="t('templateNamePlaceholder')" />
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="loadTemplateList()">{{ t('search') }}</el-button>
                        <el-button @click="resetForm(searchFormRef)">{{ t('reset') }}</el-button>
                    </el-form-item>
                </el-form>
            </el-card>

            <div class="mt-[10px]">
                <el-table :data="templateTable.data" size="large" v-loading="templateTable.loading">
                    <template #empty>
                        <span>{{ !templateTable.loading ? t('emptyData') : '' }}</span>
                    </template>
                    <el-table-column prop="template_name" :label="t('templateName')" min-width="120" />
                    <el-table-column prop="fee_type_name" :label="t('feeTypeName')" min-width="120" />
                    <el-table-column :label="t('freeShipping')" min-width="120" align="center">
                        <template #default="{ row }">
                            {{ row.is_free_shipping ? t('open') : t('close') }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="create_time" :label="t('createTime')" min-width="120" />
                    <el-table-column :label="t('operation')" fixed="right" min-width="120" align="right">
                        <template #default="{ row }">
                            <el-button type="primary" link @click="editEvent(row)">{{ t('edit') }}</el-button>
                            <el-button type="danger" link @click="deleteEvent(row.template_id)">{{ t('delete')
                            }}</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="mt-[16px] flex justify-end">
                    <el-pagination v-model:current-page="templateTable.page" v-model:page-size="templateTable.limit"
                        layout="total, sizes, prev, pager, next, jumper" :total="templateTable.total"
                        @size-change="loadTemplateList()" @current-change="loadTemplateList" />
                </div>
            </div>
        </el-card>
    </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { t } from '@/lang'
import { getShippingTemplatePageList, deleteShippingTemplate } from '@/addon/shop/api/delivery'
import { ElMessageBox, FormInstance } from 'element-plus'
import { useRoute, useRouter } from 'vue-router'

const route = useRoute()
const router = useRouter()
const pageName = route.meta.title

const templateTable = reactive({
    page: 1,
    limit: 10,
    total: 0,
    loading: true,
    data: [],
    searchParam: {
        template_name: ''
    }
})

const searchFormRef = ref<FormInstance>()

/**
 * 获取运费模板列表
 */
const loadTemplateList = (page: number = 1) => {
    templateTable.loading = true
    templateTable.page = page

    getShippingTemplatePageList({
        page: templateTable.page,
        limit: templateTable.limit,
        ...templateTable.searchParam
    }).then(res => {
        templateTable.loading = false
        templateTable.data = res.data.data
        templateTable.total = res.data.total
    }).catch(() => {
        templateTable.loading = false
    })
}
loadTemplateList()

/**
 * 添加运费模板
 */
const addEvent = () => {
    router.push({ path: '/shop/order/shipping/template_edit' })
}

/**
 * 编辑运费模板
 * @param data
 */
const editEvent = (data: any) => {
    router.push({ path: '/shop/order/shipping/template_edit', query: { id: data.template_id } })
}

/**
 * 删除运费模板
 */
const deleteEvent = (id: number) => {
    ElMessageBox.confirm(t('templateDeleteTips'), t('warning'),
        {
            confirmButtonText: t('confirm'),
            cancelButtonText: t('cancel'),
            type: 'warning'
        }
    ).then(() => {
        deleteShippingTemplate(id).then(() => {
            loadTemplateList()
        }).catch(() => {
        })
    })
}

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
    loadTemplateList()
}
</script>

<style lang="scss" scoped>
/* 多行超出隐藏 */
.multi-hidden {
    word-break: break-all;
    text-overflow: ellipsis;
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
}
</style>
